from datetime import datetime
from django.db import models
import time
from user.models import UserModel

def rand_id():
    t = str(time.time()).split(".")[0] # 获取时间戳
    return t

# 文章模型
class Article(models.Model):
    aid = models.CharField(max_length=15,primary_key=True) # 文章id
    user = models.ForeignKey(UserModel,related_name="user3s",on_delete=models.CASCADE)   # 作者id
    img = models.ImageField(upload_to="article/",default="article/default.jpg") # 照片路径
    title = models.CharField(max_length=50) # 标题
    content = models.TextField()    # 内容
    zhaiyao = models.TextField()    # 文章摘要
    createTime = models.DateTimeField(default=datetime.now,db_index=True) # 创建时间
    statu = models.IntegerField(default=0)  # 状态

    class Meta:
        db_table = "article" # 指明表名
        verbose_name = "文章表" # 在admin站点显示名称

    def __str__(self):
        return f"{self.title}"





# 分类表
class Tag(models.Model):
    tid = models.CharField(max_length=15,primary_key=True,default=rand_id,db_column="tid") # 文章id
    tag_content = models.CharField(max_length=20,db_column="tag_content")   # 分类名称

    class Meta:
        db_table = "tag"  # 指明表名
        verbose_name = "分类表"  # 在admin站点显示名称

# 文章分类关联表
class Article_tag(models.Model):
    tag = models.ForeignKey(Tag,to_field='tid', related_name="tag3s", on_delete=models.CASCADE,db_column="tag_id")
    art = models.ForeignKey(Article,to_field='aid', related_name="article3s", on_delete=models.CASCADE,db_column="art_id")

    class Meta:
        db_table = "article_tag"
        unique_together = ["tag_id", "art_id"]
        verbose_name = "文章分类关联表"  # 在admin站点显示名称

    def __str__(self):
        return f"{self.tag_id} -- {self.art_id}"